<?php

/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/15
 * Time: 9:24
 */

namespace App\Services\Api;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\CompanyModel;
use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderCheckModel;
use App\Models\Admin\OrderTempModel;
use App\Models\HezuoguanxiModel;
use App\Models\LogModel;
use App\Models\OrderCommentModel;
use App\Models\User\UserGongsiModel;
use App\Models\User\UserLevelModel;
use App\Services\Jiguang\JMessageApi;
use App\Services\OrdersPayService;
use Illuminate\Http\Request;
use App\Models\MoneyBilModel;
use DB;
use App\Models\User\UserOrdersModel;

/// App\Services\Api\Order@
class Order extends ApiBase
{


    /**
     * 插入抢单信息表
     * @param $oid
     */
    private function orderTempAdd($data = [])
    {
        if (!$data) {
            return false;
        }
        $newOrder = new OrderTempModel();
        $newOrder->bale = $data['bale'];
        $newOrder->fid = $data['fid'];
        $newOrder->cid = $data['cid'];
        $newOrder->status = $data['status'];
        $newOrder->notes = $data['notes'];
        $newOrder->out_trade_no = $data['out_trade_no'];
        $newOrder->created_at = time();
        $newOrder->updated_at = time();
        $newOrder->play_status = OrderTempModel::play_status_zhifu;
        //     $newOrder->f_level = $data['f_level'];
        $newOrder->ip = Fun::Ip();
        $newOrder->ua = Fun::UA();
        $newOrder->uid = OA::$user->getId();
        $newOrder->bond_money = $data['bond_money'];
        return $newOrder;
        /*
        if ($newOrder->save()) {
            return $newOrder;
        } else {
            return false;
        }*/
    }

    /*     * 生成支付链接 


    private function GeneratePayHref($request)
    {
        OA::Json(1, '不再使用');
        $channel = strtolower($request->input('channel', 'm'));

        $playType = $request->input('playType', 'alipay');
        $uid = OA::$user->getId();
        $out_trade_no = $request->input('out_trade_no', '');
        /// var_dump($playType,$uid,$out_trade_no);

        if ($playType !== OrdersPayService::play_alipay) {
            OA::Json(1, '目前只支持支付宝支付');
        }
        $orderTemp = OrderTempModel::where([
            'out_trade_no' => $out_trade_no,
            'uid' => $uid
        ])->where('created_at', '>', time() - OrderTempModel::MaxTime)
            ->select('id', 'uid', 'play_status', 'status', 'out_trade_no', 'money', 'bale')->first();
        if (!$orderTemp) {
            OA::Json(1, '订单不存在,或者已经失效,请返回重新抢单');
        }

        if ($orderTemp->play_status != OrderTempModel::play_status_weizhifu) {
            OA::Json(1, '订单不是未支付状态');
        }
        $orderBale = OrderBaleModel::where('id', $orderTemp['bale'])->first();
        // var_dump($orderTemp['bale'],$orderTemp);exit;
        if (!$orderBale) {
            OA::Json(1, '抢单不存在,请重新下单');
        }
        if ($orderBale['status'] !== OrderBaleModel::status_kaishi) {
            OA::Json(1, '抢单错误,请重新下单');
        }
        $subject = str_replace('&#8226;', '·', $orderBale['name']);

        $payMode = $request->input('payMode', 'directpay');
        $orderPayService = new OrdersPayService();


        $result = $orderPayService->createaAipayTrade($channel, $payMode, [
            'oid' => $orderBale['id'] ?? 0,
            'out_trade_no' => $orderTemp['out_trade_no'] ?? '', //支付宝交易号
            'subject' => $subject,
            'money' => $orderTemp['money'] ?? 0, //交易费用
            'body' => '抢单保证金', //费用描述
            'notifyUrl' => route('pay.alipay.notify'),
            'returnUrl' => route('pay.alipay.success')
        ]);
        if ($result === false) {
            OA::Json(1, '系统错误,请重试');
        }
        OA::Json(0, ['href' => $result]);
    }*/

    /**
     * 立刻抢单,如果符合等级,生成订单,如果不符合返回错误提示
     */
    public function ConfirmPayOrder($request)
    {
        $notes = $request['notes'] ?? '';
        if (strlen($notes) >= 512) {
            OA::Json(1, '备注不能太多');
        }

        $baleID = $request['id'];
        $uid = OA::$user->getId();

        ///  先判断是否允许购买

        $OrderBale = OrderBaleModel::where(['id' => $baleID])->first();
        if (!$OrderBale) {
            OA::Json(1, '订单不存在');
        }
        /*if ($OrderBale->status !== OrderBaleModel::status_kaishi) {
            OA::Json(1, '暂时不能抢单');
        }
        /*
        if ($OrderBale->period < time()) {
            OA::Json(1, '已经超过了截止时间');
        }*/
        $jonsOrderBale=$OrderBale->toArray();
        $yunxu=OrderBaleModel::yunxuqiangdan($jonsOrderBale,$uid);
         if($yunxu['code']!==0){
             OA::Json(1,$yunxu['msg']);
         }
        //自己是否已经下单,


        $one = OrderTempModel::where([
            'bale' => $baleID,
            'fid' => $uid,
            //  'play_status'=>OrderTempModel::play_status_zhifu
            // 'play_status'=>OrderTempModel::play_status_weizhifu
        ])->select('id', 'uid', 'status', 'out_trade_no')->first();
        if ($one) {
            $one->notes = $notes;
            $one->save();

            OA::Json(0, [
                'out_trade_no' => $one['out_trade_no']
            ]);
        }

        if ($OrderBale->is_rob == OrderBaleModel::is_rob_qiangdan) {///如果是抢单,则只限制3个用户
            $count = OrderTempModel::where([
                'bale' => $baleID,
            ])->count();
            if ($count >= OrderBaleModel::Max_qiangdanshu) {
                OA::Json(1, '您来晚了,抢单类型只允许前3个工厂抢单');
            }
        }
        if ($OrderBale->bond > 0) {
            $myMoney = OA::$user->getMoney();
            if ($OrderBale->bond > $myMoney) {///如果余额不足,则返回
                OA::Json(1001, $OrderBale->bond);
            }
        }


        $out_trade_no = date('Ymdhms') . mt_rand(1000, 9999);
        $TempOne = $this->orderTempAdd([
            'bale' => $baleID,
            'cid' => $OrderBale['uid'],
            'fid' => $uid,
            'out_trade_no' => $out_trade_no,
            'status' => OrderTempModel::status_qiangdanzhong,
            'created_at' => time(),
            'updated_at' => time(),
            'notes' => $notes,
            'bond_money' => $OrderBale->bond
        ]);


        if (!$this->jqMoney($TempOne, $OrderBale)) {
            OA::Json(1, $this->msgerror);
        }
        OA::Json(0, [
            'out_trade_no' => $TempOne['out_trade_no']
        ]);


    }

    private $msgerror = '';

    private function jqMoney(OrderTempModel &$orderTemp, OrderBaleModel $OrderBale)
    {

        $uid = OA::$user->getId();
        try {
            DB::beginTransaction();
            if ($orderTemp->bond_money > 0) {


                $zhuanchu = MoneyBilModel::zhuanzhangAtoB($uid, MoneyBilModel::system_uid,
                    $orderTemp->bond_money, '(' . $OrderBale->name . ')的保证金', json_encode([
                        'out_trade_no' => $orderTemp->out_trade_no,
                        'name' => $OrderBale->name
                    ]));
                $orderTemp->play_no = $zhuanchu;
            } else {
                $orderTemp->play_no = '';
            }
            $orderTemp->status = OrderTempModel::status_qiangdanzhong;
            $orderTemp->play_status = OrderTempModel::play_status_zhifu;

            if (!$orderTemp->save()) {
                $this->msgerror = '更新订单错误';
                DB::rollback();
                return false;
            }

            DB::commit();
            return true;
        } catch (\Exception $e) {
            $this->msgerror = $e->getMessage();
            DB::rollback();
            return false;
        }
    }

    /**
     * 为用户充值
     */
    public function GenchongzhiPayHref(Request $request)
    {
        $playtype = $request->get('playtype');
        $money = $request->get('value');

        if ($playtype != MoneyBilModel::PlayType_Webzhifubao &&
            $playtype != MoneyBilModel::PlayType_Appzhifubao &&
            $playtype != MoneyBilModel::PlayType_weixin) {
            OA::Json(1, '参数错误');
        }
        $res = MoneyBilModel::GenChongzhiOrder($playtype, $money);
        OA::Json(0, $res);
    }

    /**
     * 生成用户提现订单
     */
    public function tixian(Request $request)
    {
        $orderOn = MoneyBilModel::GenTixian($request->input('tixianmoney'),
            $request->input('xingming'), $request->input('zhifubao'));
        OA::Json(0, $orderOn);
    }


    /**
     * 修改订单为抢单成功状态
     */
    public function xuanzehezuo(Request $request)
    {
        $out_trade_no = $request->input('out_trade_no');
        $orderTemp = OrderTempModel::where([
            'out_trade_no' => $out_trade_no,
            'cid' => OA::$user->getid()
        ])->first();
        if (!$orderTemp) {
            OA::Json(1, '订单不存在,或者无权限');
        }
        $orderTemp->status = OrderTempModel::status_wancheng;
        $orderBale = OrderBaleModel::where([
            'id' => $orderTemp->bale,
            'uid' => OA::$user->getid()
        ])->first();
        if (!$orderBale) {
            OA::Json(1, '订单不存在,或者无权限');
        }
        if (UserOrdersModel::where([
            'order_no' => $out_trade_no
        ])->exists()) {
            OA::Json(1, '存在货单数据 ');
        }


        $orderBale->status = OrderBaleModel::status_wancheng;
        $orderBale->updated_at = time();


        /**
         * 查找未选择的工厂,返还他们的保证金
         */
        $noSelectList = OrderTempModel::where([
            'bale' => $orderTemp->bale,
            'play_status' => OrderTempModel::play_status_zhifu
        ])
            ->where('out_trade_no', '!=', $out_trade_no)
            ->select(['bale', 'out_trade_no', 'id', 'bale', 'status', 'bond_money', 'fid'])
            ->get();
        // print_r($noSelectList);exit;

        try {
            DB::beginTransaction();
            if (!$orderTemp->save()) {
                DB::rollback();
                OA::Json(1, '修改下单错误');
            }
            if (!$orderBale->save()) {
                DB::rollback();
                OA::Json(1, '修改发布订单错误');
            }
            $UserOrdersModele = new UserOrdersModel;
            $UserOrdersModele->order_no = $orderTemp->out_trade_no;
            $UserOrdersModele->bale = $orderTemp->bale;
            $UserOrdersModele->temp = $orderTemp->id;
            $UserOrdersModele->cid = $orderTemp->cid;
            $UserOrdersModele->fid = $orderTemp->fid;
            $UserOrdersModele->num = $orderBale->num;
            $UserOrdersModele->money = $orderBale->money;
            $UserOrdersModele->bond_money = $orderTemp->bond_money;
            $UserOrdersModele->kd_name = '';
            $UserOrdersModele->kd_code = '';
            $UserOrdersModele->kd_time = 0;
            $UserOrdersModele->status = UserOrdersModel::status_weifayuanliao;
            $UserOrdersModele->note = $orderTemp->notes;
            $UserOrdersModele->other = '';
            $UserOrdersModele->created_at = time();
            $UserOrdersModele->updated_at = time();
            if (!$UserOrdersModele->save()) {
                DB::rollback();
                OA::Json(1, '修改错误,创建订单错误');
            }

            $this->tuihuan($noSelectList);
            HezuoguanxiModel::addGuanxi($orderTemp->cid, $orderTemp->fid);
            DB::commit();
            JMessageApi::SendMsg($orderTemp->fid, '<a href="/h5/order/kongzhitai.html?out_trade_no=' .
                $UserOrdersModele->order_no . '"  class="msgA" >您参与的(' . $orderBale->name . ')货单,发单公司选择了和您合作生产,点击本消息查看详情</a>');
            OA::Json(0, 'ok');
        } catch (\Exception $e) {
            $this->msgerror = $e->getMessage();
            DB::rollback();
            OA::Json(1, '事务处理错误' . $this->msgerror);
        }


        OA::Json(0, 'ok');
    }


    /**
     * 保证金的退还
     */
    private function tuihuan(&$list)
    {
        foreach ($list as $vo) {
            if ($vo->play_status != OrderTempModel::play_status_fanhuan) {
                MoneyBilModel::zhuanzhangAtoB(MoneyBilModel::system_uid, $vo['fid'], $vo['bond_money'],
                    '订单号:' . $vo['out_trade_no'] . ',抢单失败,保证金返还');
                $vo->status = OrderTempModel::status_fail;
                $vo->play_status = OrderTempModel::play_status_fanhuan;
                if (!$vo->save()) {
                    throw new \Exception('订单状态设置错误');
                }
            }
        }
    }


    /**
     * 保存公司发过来的清单
     */
    public function send_yuanliao(Request $request)
    {
        $users_orders = UserOrdersModel::where([
            'order_no' => $request->input('out_trade_no'),
            'cid' => OA::$user->getId()
        ])->first();
        if (!$users_orders) {
            OA::Json(1, '订单不存在,或者无权限');
        }
        if ($users_orders->yuanliao_time != 0) {
            OA::Json(1, '系统错误,原料已经发给工厂,无法再次修改');
        }
        ///var_dump($request->input('data.array'),$request->input('data.public'));exit;
        $users_orders->yuanliao = json_encode($request->input('data.array'));
        if ($users_orders->status == UserOrdersModel::status_weifayuanliao) {
            $users_orders->status = UserOrdersModel::status_jieshouyuanliao;
        }

        if ($request->input('data.public') == 'true') {
            $users_orders->yuanliao_time = time();
        }
        if ($users_orders->save()) {
            OA::Json(0);
        } else {
            OA::Json(1, '系统错误,请重试');
        }

    }


    /**
     * 创建跟单验收记录,以及所属的用户,
     * 参数:out_trade_no,check_type="cid|fid" cid表示公司 fid表示工厂
     */
    public function comCheckUid(Request $request)
    {
        $out_trade_no = $request['out_trade_no'] ?? '';
        if ($out_trade_no == '') {
            Fun::showmsg("参数错误");
        }
        $check_type = $request['check_type'] ?? '';
        if ($check_type !== 'cid' && $check_type !== 'fid') {
            Fun::showmsg("参数错误");
        }
        $orderTempModel = OrderTempModel::where(['out_trade_no' => $out_trade_no])->first();
        if (!$orderTempModel) {
            Fun::showmsg("订单不存在");
        }
        $uid = OA::$user->getId();
        // var_dump($orderTempModel->cid, $uid);exit;
        if ($orderTempModel->cid != $uid) {
            Fun::showmsg("没有修改的权限");
        }
        $orderOrder = UserOrdersModel::where([
            'order_no' => $out_trade_no
        ])->select([
            'id', 'order_no', 'check_uid'
        ])->first();
        if ($check_type == 'fid') {
            $orderOrder->check_uid = $orderTempModel->fid;
        } elseif ($check_type == 'cid') {
            $orderOrder->check_uid = $orderTempModel->cid;
        } else {
            Fun::showmsg("参数错误");
        }

        /*
        $ordercheck = OrderCheckModel::where(['out_trade_no' => $out_trade_no])->first();
        if ($ordercheck) {
            Fun::showmsg("跟单验货报告已经设置过所属权限,无法再次修改");
        }
        $ordercheck = new OrderCheckModel;
        $ordercheck->out_trade_no = $out_trade_no;
        $ordercheck->stylelist = '';
        $ordercheck->sizelist = '';
        $ordercheck->thrumlist = '';
        $ordercheck->detail = '';
        $ordercheck->staffTotalNum = 0;
        $ordercheck->staffStyleNum = 0;
        $ordercheck->del = 0;
        $ordercheck->created_at = time();
        $ordercheck->updated_at = time();
        $ordercheck->other_stylelist = 0;
        $ordercheck->other_sizelist = '';
        $ordercheck->other_thrumlist = '';
        $ordercheck->com_name = '';
        $ordercheck->com_tel = '';
        if ($check_type == 'fid') {
            $ordercheck->check_uid = $orderTempModel->fid;
        } elseif ($check_type == 'cid') {
            $ordercheck->check_uid = $orderTempModel->cid;
        } else {
            Fun::showmsg("参数错误");
        }
        $save = $ordercheck->save();
        */
        if ($orderOrder->save()) {
            OA::Json(0);
        } else {
            OA::Json(1, '系统错误,请重试');
        }

    }


    /**
     * 保存跟单表单数据
     */
    public function savecomCheck(Request $request)
    {
        $out_trade_no = $request['out_trade_no'] ?? '';
        if ($out_trade_no == '') {
            Fun::showmsg("参数错误");
        }


        $orderOrder = UserOrdersModel::where([
            'order_no' => $out_trade_no,
            'check_uid' => OA::$user->getId()
        ])->select([
            'id', 'order_no', 'check_uid'
        ])->first();

        if (!$orderOrder) {
            Fun::showmsg("验货单不存在,或者权限不允许");
        }
        //   var_dump($request->input('stylelist'));exit;
        $data = Fun::date(time(), false);
        $OrderCheckModel = OrderCheckModel::where([
            'out_trade_no' => $out_trade_no,
            'date' => $data
        ])
            ->orderBy('id', 'desc')
            ->first();
        if ($OrderCheckModel) {
            $ordercheck = $OrderCheckModel;
        } else {
            $ordercheck = new OrderCheckModel;
            $ordercheck->date = $data;
            $ordercheck->out_trade_no = $out_trade_no;
            $ordercheck->created_at=time();
        }
        $ordercheck->stylelist = $request->input('stylelist');
        $ordercheck->sizelist = $request->input('sizelist');
        $ordercheck->thrumlist = $request->input('thrumlist');

        $ordercheck->detail = Fun::encode($request->input('detail'));
        $ordercheck->staffTotalNum = Fun::encode($request->input('staffTotalNum'));
        $ordercheck->wanchengdu = Fun::encode($request->input('wanchengdu'));
        if ($ordercheck->wanchengdu >= 100) {
            $ordercheck->wanchengdu = 100;
        }
        $ordercheck->staffStyleNum = Fun::encode($request->input('staffStyleNum'));
        $ordercheck->finishTime = strtotime($request->input('finishTime'));

        $ordercheck->fuzeren_name = Fun::encode($request->input('fuzeren_name'));
        $ordercheck->fuzeren_tel = Fun::encode($request->input('fuzeren_tel'));
        $ordercheck->updated_at = time();
        if ($ordercheck->save()) {
            OA::Json(0);
        } else {
            OA::Json(1, '系统错误,请重试');
        }
    }


    public function order_fabuo(Request $request)
    {
        $out_trade_no = $request['out_trade_no'] ?? '';
        if ($out_trade_no == '') {
            Fun::showmsg("参数错误");
        }
        $users_orders = UserOrdersModel::where([
            'order_no' => $out_trade_no,
            'fid' => OA::$user->getId()
        ])->first();
        if (!$users_orders) {
            OA::Json(1, '订单不存在,或者无权限');
        }
        $users_orders->receiving_address = $request->input('receiving_address');
        $users_orders->send_address = $request->input('send_address');
        $users_orders->kd_name = $request->input('kd_name');
        $users_orders->kd_code = $request->input('kd_code');
        $users_orders->kd_time = time();
        $users_orders->status = UserOrdersModel::status_dabaofahuo;
        if ($users_orders->save()) {
            OA::Json(200);
        } else {
            OA::Json(1, '系统错误,请重试');
        }
    }

    /**
     *  确认收货
     */
    public function querenshouhuo(Request $request)
    {
        $out_trade_no = $request['out_trade_no'] ?? '';
        if ($out_trade_no == '') {
            Fun::showmsg("参数错误");
        }
        $users_orders = UserOrdersModel::where([
            'order_no' => $out_trade_no,
            'cid' => OA::$user->getId()
        ])->first();
        if (!$users_orders) {
            OA::Json(1, '订单不存在,或者无权限');
        }
        $users_orders->status = UserOrdersModel::status_daohuoqianshou;
        if ($users_orders->save()) {
            OA::Json(0);
        } else {
            OA::Json(1, '系统错误,请重试');
        }
    }

    /**
     * 设置开始生产的状态
     */
    public function Kaishishengchan(Request $request)
    {
        $out_trade_no = $request['out_trade_no'] ?? '';
        if ($out_trade_no == '') {
            Fun::showmsg("参数错误");
        }
        $users_orders = UserOrdersModel::where([
            'order_no' => $out_trade_no,
            'fid' => OA::$user->getId()
        ])->first();
        if (!$users_orders) {
            OA::Json(1, '订单不存在,或者无权限');
        }
        $users_orders->status = UserOrdersModel::status_shengchanzhong;
        if ($users_orders->save()) {
            OA::Json(0);
        } else {
            OA::Json(1, '系统错误,请重试');
        }
    }

    /**
     * 设置订单为完成状态,返回保证金
     */
    public function orderwancheng(Request $request)
    {
        $out_trade_no = $request['out_trade_no'] ?? '';
        if ($out_trade_no == '') {
            Fun::showmsg("参数错误");
        }
        $users_orders = UserOrdersModel::where([
            'order_no' => $out_trade_no,
            'cid' => OA::$user->getId()
        ])->first();
        if (!$users_orders) {
            OA::Json(1, '订单不存在,或者无权限');
        }
        if ($users_orders->status == UserOrdersModel::status_wancheng) {
            OA::Json(1, '订单已经设置过了,无法再次设置');
        }

        $OrderBaleModel = OrderBaleModel::where('id', $users_orders->bale)->first();
        if (!$OrderBaleModel) {
            OA::Json(1, '订单不存在');
        }

        if ($users_orders->bond_money == 0) {
            $users_orders->status = UserOrdersModel::status_wancheng;
            if ($users_orders->save()) {
                OA::Json(0);
            }
        }

        try {
            DB::beginTransaction();
            $users_orders->status = UserOrdersModel::status_wancheng;
            $users_orders->complete = UserOrdersModel::complete_yes;

            MoneyBilModel::zhuanzhangAtoB(MoneyBilModel::system_uid, $users_orders->fid, $users_orders->bond_money,
                '货单名称:(' . $OrderBaleModel->name . '),订单号:' . $users_orders->order_no . '保证金返还' . $users_orders->bond_money . '元', $OrderBaleModel->name);

            if (!$users_orders->save()) {
                //OA::Json(0);
                throw new \Exception('订单状态保存错误');
            }


            DB::commit();
            JMessageApi::SendMsg($users_orders->fid, "您参与生产的货单名称:(" . $OrderBaleModel->name . ")已经被发单方设置为完成.");
            OA::Json(0);

        } catch (\Exception $e) {
            DB::rollback();
            \Log::error($e->getMessage());
            LogModel::add($e->getMessage(), LogModel::typeError);
            OA::Json(1, '设置错误,请联系管理员');
        }


    }


    /**
     * 获取当前用户的余额
     */
    public function getmoney()
    {
        return OA::$user->getMoney();
    }


    /**
     * 获取跟单报告
     */
    public function comCheck(Request $request)
    {
        $out_trade_no = $_POST['out_trade_no'] ?? '';
        $lishiid = $_POST['lishiid'] ?? '';
        if ($out_trade_no == '') {
            OA::Json(1, "参数错误");
        }
        $orderTempModel = OrderTempModel::where(['out_trade_no' => $out_trade_no])->first();
        if (!$orderTempModel) {
            OA::Json(1, "订单不存在");
        }
        $out_trade_no = $orderTempModel->out_trade_no;
        $uid = OA::$user->getId();
        $type = null; ///gs表示公司  gc 表示工厂
        if ($orderTempModel->cid == $uid) {
            $type = 'gs';    ///表示公司
        } elseif ($orderTempModel->fid == $uid) {
            $type = 'gc';    ///表示工厂
        } else {
            OA::Json(1, "您无权限操作表单");
        }
        $find=OrderCheckModel::where(['out_trade_no' => $out_trade_no]);
        if(!empty($lishiid)){
            $find->where(['id'=>$lishiid]);
        }
        $OrderCheckModel = $find
            ->orderBy('id', 'desc')
            ->first();
        $OrderBales = OrderBaleModel::where([
            'id' => $orderTempModel->bale
        ])->select([
            'id', 'name', 'period'
        ])
            ->first();
        /*
        $check_uid = '';
        if ($OrderCheckModel && $OrderCheckModel->check_uid == OA::$user->getId()) {
            $check_uid = 'true';
        }*/


        $Com_name = UserGongsiModel::getCom_name( $orderTempModel->cid);

        $orderOrder = UserOrdersModel::where([
            'order_no' => $out_trade_no
        ])->select([
            'id', 'order_no', 'check_uid'
        ])->first();
        if (!$orderOrder['check_uid']) {
            $orderOrder['check_uid'] = 0;
        }


        $read_only = true;
        if ($orderOrder && $orderOrder->check_uid != 0) {
            $read_only = $orderOrder->check_uid != OA::$user->getId();
        } elseif ($type == 'gs') {
            $read_only = false;
        }
        $list = [
            'out_trade_no' => $orderTempModel->out_trade_no,
            'name' => $OrderBales['name'],
            'com_name' => $Com_name,
            'period' => Fun::date($OrderBales['period'], false),
            'type' => $type,
            'read_only' => $read_only
        ];
        $list['check_uid'] = $orderOrder['check_uid'];


        $OrderCheck = false;
        if ($OrderCheckModel) {
            $OrderCheck = [];
            $OrderCheck['detail'] = $OrderCheckModel['detail'] ?? '';
            ///    $OrderCheck['check_uid'] = $OrderCheckModel['check_uid'] ?? '';
            $OrderCheck['fuzeren_name'] = $OrderCheckModel['fuzeren_name'] ?? '';
            $OrderCheck['fuzeren_tel'] = $OrderCheckModel['fuzeren_tel'] ?? '';
            $OrderCheck['stylelist'] = $OrderCheckModel['stylelist'] ?? '';
            $OrderCheck['sizelist'] = $OrderCheckModel['sizelist'] ?? '';
            $OrderCheck['thrumlist'] = $OrderCheckModel['thrumlist'] ?? '';
            $OrderCheck['staffTotalNum'] = $OrderCheckModel['staffTotalNum'] ?? '';
            $OrderCheck['wanchengdu'] = $OrderCheckModel['wanchengdu'] ?? '';
            $OrderCheck['staffStyleNum'] = $OrderCheckModel['staffStyleNum'] ?? '';
            $OrderCheck['finishTime'] = Fun::date($OrderCheckModel['finishTime'], false);
        }
        $list['info'] = $OrderCheck;


        OA::Json(0, $list);
    }


    public function comCheck_lishi(Request $request)
    {
            $out_trade_no = $_POST['out_trade_no'] ?? '';
            if ($out_trade_no == '') {
                OA::Json(1, "参数错误");
            }
            $orderTempModel = OrderTempModel::where(['out_trade_no' => $out_trade_no])->first();
            if (!$orderTempModel) {
                OA::Json(1, "订单不存在");
            }
            $out_trade_no = $orderTempModel->out_trade_no;
            $uid = OA::$user->getId();
            $type = null; ///gs表示公司  gc 表示工厂
            if ($orderTempModel->cid == $uid) {
                $type = 'gs';    ///表示公司
            } elseif ($orderTempModel->fid == $uid) {
                $type = 'gc';    ///表示工厂
            } else {
                OA::Json(1, "您无权限操作表单");
            }
            $page=$_POST['page']??0;
            $listCheck=OrderCheckModel::where([
                'out_trade_no'=>$out_trade_no,
                'del'=>0
            ])->select([
                'finishTime','created_at','wanchengdu','id','out_trade_no'
            ])
                ->orderBy('id','desc')
                ->skip($page*10)
                ->take(10)
                ->get()->toArray();
            foreach ($listCheck as $k=>$v){
                $listCheck[$k]['finishTime']=Fun::date($listCheck[$k]['finishTime'],false);
                $listCheck[$k]['created_at']=Fun::date($listCheck[$k]['created_at']);
            }
            OA::Json(0,$listCheck);
    }


}
